import sys

def getTopUrls(url_dict, n):
    sorted_items = sorted(url_dict.items(), key=lambda item: (-item[1], item[0]))
    return [item[0] for item in sorted_items[:n]]
        
def main():
    url_dict = {}
    ans = []
    for line in sys.stdin:
        line = line.strip()
        if line.isdigit():
            ans = getTopUrls(url_dict, int(line))
            print(",".join(ans))
        else:
            url_dict[line] = url_dict.get(line, 0) + 1

if __name__ == "__main__":
    main()